import { defineStore } from "pinia";
import { urlConf } from "@/config/resource";
import { adOneCardApi } from "@/api/app";

export const commonStore = defineStore("common", {
  unistorage: true, // 是否持久化到内存
  state: () => {
    return {
      urlConf,
      privacyOpen: false, // 隐私弹窗
      isPopupOpen: false, // popup弹窗
      isDisabled: false, // 是否允许关闭弹窗
      popupType: "skill", // 弹窗类型  serve、share、contact、skill、prompt
      contactParams: {}, // 联系方式弹窗所需参数
      promptParams: {}, // 提示弹窗所需参数
      submitParams: {}, // 开启相亲弹窗所需参数
      shareItems: [1, 2], //底部分享弹窗
      purchaseParams: {}, //购买弹窗所需参数
      serveParams: {}, //客服弹窗所需参数
      uploadImg: "", //上传照片
      giftStatus: false, //赠送礼物
      videoAd: null, //激励视频实例
      adStatus: false, //判断广告是否拉取成功
      pageVideoAd: {}, //不同页面的广告实例配置
    };
  },
  actions: {
    updatePrivacyOpen(bool) {
      this.privacyOpen = bool;
    },
    updateContactParams(data) {
      this.contactParams = data;
    },
    updatePromptParams(data) {
      this.promptParams = data;
    },
    updateSubmitParams(bool) {
      this.submitParams = {
        title: bool ? "重新开启相亲" : "",
        content: bool ? "重新开启相亲后，可立即恢复查看电话，是否开启？" : "",
      };
    },
    updatePopupDisabled(bool) {
      this.isDisabled = bool;
    },
    updateShareParams(data) {
      this.shareItems = data;
    },
    updatePopupOpen(bool, type) {
      this.isPopupOpen = bool;
      this.popupType = type || "";
    },
    // 购买结果弹窗
    updatePurchaseParams(data) {
      this.purchaseParams = data;
    },
    updateServeParams(data = {}) {
      this.serveParams = data;
    },
    updateUploadImg(url) {
      this.uploadImg = url || "";
    },
    updateGiftStatus(bool) {
      this.giftStatus = bool;
    },

    // 创建广告实例
    initAdCase(page) {
      this.pageVideoAd[page] = null;
      if (wx.createRewardedVideoAd) {
        this.pageVideoAd[page] = wx.createRewardedVideoAd({
          adUnitId: "adunit-3000fd2b5a5ee6d4",
        });
        this.pageVideoAd[page].onLoad(() => {
          this.adStatus = true;
          console.log("广告拉取成功");
        });
        this.pageVideoAd[page].onError((err) => {
          console.error("激励视频广告加载失败", err);
        });
        this.pageVideoAd[page].onClose((res) => {
          if (res && res.isEnded) {
            // 正常播放结束，可以下发游戏奖励
            this.popupType = "close";
            this.isPopupOpen = false;
            uni.showLoading({
              title: '加载中'
            });
            
            adOneCardApi()
              .then((res) => {
                if (res.code == 1) {
                  setTimeout(() => {
                    this.giftStatus = true;
                    this.isPopupOpen = true;
                    this.popupType = "gift";
                    uni.hideLoading()
                  }, 200);
                }
              })
              .catch((err) => {});
          } else {
            // 播放中途退出，不下发游戏奖励
            uni.showToast({
              title: "需要完整看完广告才能领取奖励",
              icon: "none",
            });
          }
        });
      }
    },

    // 用户触发广告后，显示激励视频广告
    openAdCase(page) {
      this.giftStatus = false;
      if (this.pageVideoAd[page]) {
        this.pageVideoAd[page].show().catch(() => {
          // 失败重试
          this.pageVideoAd[page]
            .load()
            .then(() => this.pageVideoAd[page].show())
            .catch((err) => {
              console.error("激励视频 广告显示失败", err);
            });
        });
      }
    },
  },
});
